<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Order.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Order_Models_Dao_Pdo_Mysql_Order extends Tomato_Model_Dao
	implements Order_Models_Interface_Order
{
	public function convert($entity) 
	{
		return new Order_Models_Order($entity); 
	}
	// Tìm mã số tiếp theo của sản phẩm thêm mới vào
	public function getNextId(){		
		$config 					= Tomato_Config::getConfig(); 
		$prefix_code_order		= $config->get('prefix_code')->order_code;
		$prefix_code_order_length = strlen($prefix_code_order);
		$name_ini_length			= 'order_code_length';
		$length_max_id				= $config->get('prefix_code')->$name_ini_length;
			
		$select = $this->_conn
					->select()
					->from(array('p' => $this->_prefix . 'order'),'MAX(SUBSTR(p.id,'.($prefix_code_order_length+1).')) as MAX_ID');
		$row = $select->query()
			   		  ->fetch();	
		$max_id =  (null == $row) ? 0 : $row->MAX_ID;
		$max_id++;
		$addition_zero_num 		= $length_max_id - strlen($max_id) - strlen($prefix_code_order);
		
		$code = $prefix_code_order;
		for($i=1;$i<=$addition_zero_num;$i++)
			$code.='0';
		$code.= $max_id;
		return $code;	
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'order'));
		if ($exp) {
            if (isset($exp['order_id'])) {
				$select->where("c.order_id=?",$exp['order_id']);
			}
		}		
        $select->order('c.id DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix . 'order'), array('num_order' => 'COUNT(*)'));
        /*
        if ($exp) {
            if (isset($exp['>>>'])) {
                $select->where("c.>>>=?",$exp[>>>]);
            }
        }*/
        return $select->query()->fetch()->num_order;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'order'))
					->where('c.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Order_Models_Order($row);
	}

	public function add($order) 
	{/*
			$data = array(
				'name'		   => $order->name,
				>>>	
			);
			
			if (isset($order->id) && $order->id != null) {
				$data['id'] = $order->id;
				$this->_conn->insert($this->_prefix . 'order', $data);
				return $order->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'order', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'order');
			}
		}
		*/
	}
	
	public function update($order) 
	{/*
		$this->_conn->update($this->_prefix . 'order', 
							array(
								'name' 			=> $order->name,
								>>>	
							), 
							array(
								'id = ?' => $order->id,
							));
	*/
	}
	
	public function delete($order) 
	{/*
		$this->_conn->delete($this->_prefix . 'order', 
							array(
								'id = ?' => $order->id,
							));
	*/						
	}	
}